package com.ruoyi.quartz.task.mqtt;


import com.ruoyi.quartz.task.devicupdatetemperature.AutoUpdatePointAndDeviceJob;
import com.ruoyi.yanjing.domain.ElectricityPole;
import com.ruoyi.yanjing.service.IElectricityPoleService;
import com.ruoyi.yanjing.service.MqttService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component("autoAddMqttTopicJob")
public class AutoAddMqttTopicJob implements Runnable {

    private static final Log log = LogFactory.getLog(AutoAddMqttTopicJob.class);

    @Autowired
    IElectricityPoleService iElectricityPoleService;

    @Autowired
    MqttService mqttService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized void run() {
        log.info("Auto add topic start:==============");
        List<ElectricityPole> electricityPoles = iElectricityPoleService.selectElectricityPoleGatewayIdList();
        if(electricityPoles != null && electricityPoles.size() > 0){
            for (ElectricityPole electricityPole : electricityPoles) {
                log.info("Auto add topic : " + electricityPole.getGatewayId());
                mqttService.addTopic(electricityPole);
            }
        }
        log.info("Auto add topic end:==============");
    }

}
